home *** CD-ROM | disk | FTP | other *** search
- #include <Gestalt.h>
- #include <MacTypes.h>
- #include <Resources.h>
- #include <Traps.h>
- #include <MixedMode.h>
- #include <Processes.h>
-
- #include "tear.h"
- #include "wdef.h"
-
- #include "main_i.h"
-
- static InitGrafUPP gSaveInitGraf;
- static ShowHideUPP gSaveShowHide;
-
- static OSType **gMSAppList;
- static UInt32 gMSAppCount;
-
- static UniversalProcPtr ApplyTrapPatch (short trap, UniversalProcPtr patchPtr)
- {
- UniversalProcPtr trapPtr;
-
- if (patchPtr == nil)
- return nil;
-
- trapPtr = NGetTrapAddress (trap, (trap & 0x0800) ? ToolTrap : OSTrap);
- NSetTrapAddress (patchPtr, trap, (trap & 0x0800) ? ToolTrap : OSTrap);
-
- return trapPtr;
- }
-
- static bool HaveProcessManager(void)
- {
- static bool alreadyKnowWeHaveIt = false;
- SInt32 response;
- OSErr error;
-
- if (alreadyKnowWeHaveIt)
- return true;
-
- error = Gestalt (gestaltOSAttr, &response);
-
- if ((error != noErr) || ((response & gestaltLaunchControl) == 0)) {
- return false;
- }
-
- alreadyKnowWeHaveIt = true;
-
- return true;
- }
-
- static bool is_MS_app(OSType signature)
- {
- UInt32 i;
- OSType *p;
-
- for (i = 0, p = *gMSAppList; i < gMSAppCount; i++, p++)
- {
- if (signature == *p)
- return true;
- }
-
- return false;
- }
-
- static bool prepare_app_list(void)
- {
- Handle h;
-
- h = Get1Resource(kMSAppListRsrcType, 128);
- if (h != nil)
- {
- UInt32 len = GetHandleSize(h);
-
- gMSAppCount = (len / sizeof(OSType));
-
- gMSAppList = (OSType**)(NewHandleSys(len));
- if (gMSAppList != nil)
- {
- BlockMoveData(*h, *gMSAppList, len);
- return (gMSAppCount > 0);
- }
- }
-
- return false;
- }
-
- #pragma mark -
-
- static void Patched_ShowHide(WindowRef w, Boolean showOrHide)
- {
- // slam the window's defproc handle to point to our stuff
- // ...if necessary.
-
- FixUpWDEF(w);
-
- CallShowHideProc(gSaveShowHide, w, showOrHide);
- }
-
- static void Patched_InitGraf (void* ioGlobals)
- {
- ProcessSerialNumber currentProcess = { 0, kCurrentProcess };
- ProcessInfoRec processInfo;
-
- processInfo.processInfoLength = sizeof (processInfo);
- processInfo.processAppSpec = nil;
- processInfo.processName = nil;
-
- if (HaveProcessManager() &&
- (GetProcessInformation (¤tProcess, &processInfo) == noErr) &&
- is_MS_app(processInfo.processSignature))
- {
- gSaveShowHide = ApplyTrapPatch (_ShowHide, NewShowHideProc (Patched_ShowHide));
- }
-
- CallInitGrafProc (gSaveInitGraf, ioGlobals);
- }
-
- #pragma mark -
-
- void main (void)
- {
- Handle initCode = nil;
- THz theZone;
-
- /* Detach and lock the code resource */
-
- initCode = Get1Resource ('INIT', 0);
- if (initCode == nil)
- goto failure;
-
- DetachResource (initCode);
- if (ResError () != noErr)
- goto failure;
-
- if (PrepareTearRgn() != noErr)
- goto failure;
-
- if (! prepare_app_list())
- goto failure;
-
- theZone = GetZone();
- SetZone (SystemZone());
-
- HLockHi (initCode);
-
- /* Install the InitGrafPatch */
-
- gSaveInitGraf = (InitGrafUPP) ApplyTrapPatch (_InitGraf, NewInitGrafProc (Patched_InitGraf));
-
- failure:;
- }
-
-